home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / KPlib 1.3.5 / KPStack.h < prev    next >
Text File  |  1995-12-17  |  5KB  |  192 lines

  1. // A module of KPlib v1.3.5.
  2. // Written by Keith Pomakis during the summer of 1994.
  3. // Released to the public domain on October 10, 1994.
  4.  
  5. #ifndef KP_STACK_DEFINED
  6. #define KP_STACK_DEFINED
  7.  
  8. #include "KPbasic.h"
  9. #include "KPList.h"
  10. #include <stdlib.h>
  11.  
  12. // Assumes Element has a default constructor and operator=().
  13.  
  14. template <class Element>
  15. class KPStack {
  16.     public:
  17.         KPStack();
  18.         KPStack(const KPStack<Element> &stack);
  19.         KPStack(const KPList<Element> &list);
  20.         KPStack(const Element &element);
  21.         ~KPStack();
  22.         operator KPList<Element>() const;
  23.         KPStack<Element> &operator=(const KPStack<Element> &stack);
  24.         KPStack<Element> &operator=(const Element &element);
  25.         KPStack<Element> &push(const Element &element);
  26.         Element pop();
  27.         Element &top();
  28.         const Element &top() const;
  29.         int size() const;
  30.         bool is_empty() const;
  31.         KPStack<Element> &clear();
  32.     protected:
  33.         static void stack_empty_error(const char *fname);
  34.         KPList<Element> my_list;
  35. };
  36.  
  37. /****************************************************************************/
  38.  
  39. template <class Element>
  40. inline
  41. KPStack<Element>::KPStack(): my_list()
  42. { /* do nothing */ }
  43.  
  44. /****************************************************************************/
  45.  
  46. template <class Element>
  47. inline
  48. KPStack<Element>::KPStack(const KPStack<Element> &stack): my_list(stack.my_list)
  49. { /* do nothing */ }
  50.  
  51. /****************************************************************************/
  52.  
  53. template <class Element>
  54. inline
  55. KPStack<Element>::KPStack(const KPList<Element> &list): my_list(list)
  56. { /* do nothing */ }
  57.  
  58. /****************************************************************************/
  59.  
  60. template <class Element>
  61. inline
  62. KPStack<Element>::KPStack(const Element &element): my_list(element)
  63. { /* do nothing */ }
  64.  
  65. /****************************************************************************/
  66.  
  67. template <class Element>
  68. inline
  69. KPStack<Element>::~KPStack()
  70. {
  71.     my_list.clear();
  72. }
  73.  
  74. /****************************************************************************/
  75.  
  76. template <class Element>
  77. inline
  78. KPStack<Element>::operator KPList<Element>() const
  79. {
  80.     return my_list;
  81. }
  82.  
  83. /****************************************************************************/
  84.  
  85. template <class Element>
  86. inline KPStack<Element> &
  87. KPStack<Element>::operator=(const KPStack<Element> &stack)
  88. {
  89.     my_list = stack.my_list;
  90.     return *this;
  91. }
  92.  
  93. /****************************************************************************/
  94.  
  95. template <class Element>
  96. inline KPStack<Element> &
  97. KPStack<Element>::operator=(const Element &element)
  98. {
  99.     my_list = element;
  100.     return *this;
  101. }
  102.  
  103. /****************************************************************************/
  104.  
  105. template <class Element>
  106. inline KPStack<Element> &
  107. KPStack<Element>::push(const Element &element)
  108. {
  109.     my_list.add_to_head(element);
  110.     return *this;
  111. }
  112.  
  113. /****************************************************************************/
  114.  
  115. template <class Element>
  116. inline Element
  117. KPStack<Element>::pop()
  118. {
  119.     if (my_list.size() < 1)
  120.         stack_empty_error("pop()");
  121.     
  122.     Element element = my_list.head();
  123.     my_list.remove_head();
  124.     return element;
  125. }
  126.  
  127. /****************************************************************************/
  128.  
  129. template <class Element>
  130. inline Element &
  131. KPStack<Element>::top()
  132. {
  133.     if (my_list.size() < 1)
  134.         stack_empty_error("top()");
  135.  
  136.     return my_list.head();
  137. }
  138.  
  139. /****************************************************************************/
  140.  
  141. template <class Element>
  142. inline const Element &
  143. KPStack<Element>::top() const
  144. {
  145.     if (my_list.size() < 1)
  146.         stack_empty_error("top()");
  147.  
  148.     return my_list.head();
  149. }
  150.  
  151. /****************************************************************************/
  152.  
  153. template <class Element>
  154. inline int
  155. KPStack<Element>::size() const
  156. {
  157.     return my_list.size();
  158. }
  159.  
  160. /****************************************************************************/
  161.  
  162. template <class Element>
  163. inline bool
  164. KPStack<Element>::is_empty() const
  165. {
  166.     return my_list.is_empty();
  167. }
  168.  
  169. /****************************************************************************/
  170.  
  171. template <class Element>
  172. inline KPStack<Element> &
  173. KPStack<Element>::clear()
  174. {
  175.     my_list.clear();
  176.     return *this;
  177. }
  178.  
  179. /****************************************************************************/
  180.  
  181. template <class Element>
  182. void
  183. KPStack<Element>::stack_empty_error(const char *fname)
  184. {
  185.     cerr << "KPStack: " << fname << " - stack empty\n";
  186.     exit(EXIT_FAILURE);
  187. }
  188.  
  189. /****************************************************************************/
  190.  
  191. #endif /* KP_STACK_DEFINED */
  192.